package com.itboye.textileexpres.main;

import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseFragment;
import com.itboye.textileexpres.login.LoginActivity;
import com.itboye.textileexpres.myset.ActivityCustomerService;
import com.itboye.textileexpres.order.ActivityOrderSearch;
import com.itboye.textileexpres.order.FragmentMyOrder;
import com.itboye.textileexpres.order.FragmentOrderReceiptOfGood;
import com.itboye.textileexpres.order.FragmentOrderWanchengGood;
import com.itboye.textileexpres.order.FragmentOrderWhole;
import com.itboye.textileexpres.order.FragmentToBeHippedOrder;
import com.itboye.textileexpres.order.MyOrderShaerActivity;
import com.itboye.textileexpres.order.OrderViewPagerAdapter;
import com.itboye.textileexpres.util.IsMobileNumPhone;

public class OrderFragment extends BaseFragment implements OnClickListener {
	TextView title;
	ImageView back, img_other, img_search;

	RelativeLayout all_rel, pay_rel, Ship_rel, receive_rel, finish_rel;
	ArrayList<Fragment> fragmentList;
	float f;
	private int currIndex;
	View all_view, pay_view, Ship_view, receive_view, finish_view;
	TextView all_tv, pay_tv, Ship_tv, receive_tv, finish_tv, tvSearch;

	LinearLayout linsearch;
	LinearLayout lineTop, lineCenter,lineLoginBut;
	ViewPager viewpager;
	LinearLayout lineTopLogin;
	ImageView imgTopLogin;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_order;
	}

	@Override
	public void onMyActivityCreated() {
		// TODO Auto-generated method stub
		title.setText(R.string.Order);
		back.setVisibility(View.GONE);
		img_other.setVisibility(View.GONE);

		linsearch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						MyOrderShaerActivity.class);
				startActivity(intent);
			}
		});
		tvSearch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						MyOrderShaerActivity.class);
				startActivity(intent);
			}
		});
		initRelative();
		initViewPager();
		// 登录返回广播
		IntentFilter intentFilterAvart2 = new IntentFilter("LoginSuccess");
		getActivity().registerReceiver(avartBroadcastReceiver,
				intentFilterAvart2);
		// 退出广播
		IntentFilter intentFilterAvart3 = new IntentFilter("project");
		getActivity().registerReceiver(start, intentFilterAvart3);
		
		// 退出广播
			

		if (IsMobileNumPhone.isUid() == null || IsMobileNumPhone.isUid() == "") {
			lineLoginBut.setVisibility(View.GONE);
//			lineCenter.setVisibility(View.GONE);
//			viewpager.setVisibility(View.GONE);
			lineTopLogin.setVisibility(View.VISIBLE);
		} else {
//			lineTop.setVisibility(View.VISIBLE);
//			lineCenter.setVisibility(View.VISIBLE);
			lineLoginBut.setVisibility(View.VISIBLE);
			lineTopLogin.setVisibility(View.GONE);
			initViewPager();
		}

	}
	BroadcastReceiver updateorder = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {

			initRelative();
			initViewPager();

		};
	};
	
	BroadcastReceiver avartBroadcastReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {

			lineLoginBut.setVisibility(View.VISIBLE);
			lineTopLogin.setVisibility(View.GONE);

		};
	};
	BroadcastReceiver start = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			lineLoginBut.setVisibility(View.GONE);
			lineTopLogin.setVisibility(View.VISIBLE);

		};
	};

	private void initViewPager() {
		// TODO Auto-generated method stub
		fragmentList = new ArrayList<Fragment>();
		Fragment allFragment = new FragmentOrderWhole(0);// 全部
		Fragment payFragment = new FragmentMyOrder(1);// 代付款
		Fragment sendFragment = new FragmentToBeHippedOrder(2);// 待发货
		Fragment receiveFragment = new FragmentOrderReceiptOfGood(3);// 待收货
		Fragment finishFragment = new FragmentOrderWanchengGood(4);// 已经完成
		fragmentList.add(allFragment);
		fragmentList.add(payFragment);
		fragmentList.add(sendFragment);
		fragmentList.add(receiveFragment);
		fragmentList.add(finishFragment);
		viewpager.setAdapter(new OrderViewPagerAdapter(
				getChildFragmentManager(), fragmentList));

		viewpager.setCurrentItem(currIndex);// 设置当前显示标签页为第一页
		setTextColorTitle(currIndex);
		viewpager.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	@SuppressWarnings("deprecation")
	private void setTextColorTitle(int i) {
		// TODO Auto-generated method stub
		switch (i) {
		case 0:
			all_view.setVisibility(View.VISIBLE);
			pay_view.setVisibility(View.GONE);
			Ship_view.setVisibility(View.GONE);
			receive_view.setVisibility(View.GONE);
			finish_view.setVisibility(View.GONE);
			all_tv.setTextColor(getResources().getColor(R.color.main_color));
			pay_tv.setTextColor(getResources().getColor(R.color.black));
			receive_tv.setTextColor(getResources().getColor(R.color.black));
			finish_tv.setTextColor(getResources().getColor(R.color.black));
			Ship_tv.setTextColor(getResources().getColor(R.color.black));
			break;
		case 1:
			all_view.setVisibility(View.GONE);
			pay_view.setVisibility(View.VISIBLE);
			Ship_view.setVisibility(View.GONE);
			receive_view.setVisibility(View.GONE);
			finish_view.setVisibility(View.GONE);
			all_tv.setTextColor(getResources().getColor(R.color.black));
			pay_tv.setTextColor(getResources().getColor(R.color.main_color));
			receive_tv.setTextColor(getResources().getColor(R.color.black));
			finish_tv.setTextColor(getResources().getColor(R.color.black));
			Ship_tv.setTextColor(getResources().getColor(R.color.black));
			break;
		case 2:
			all_view.setVisibility(View.GONE);
			pay_view.setVisibility(View.GONE);
			Ship_view.setVisibility(View.VISIBLE);
			receive_view.setVisibility(View.GONE);
			finish_view.setVisibility(View.GONE);
			all_tv.setTextColor(getResources().getColor(R.color.black));
			pay_tv.setTextColor(getResources().getColor(R.color.black));
			Ship_tv.setTextColor(getResources().getColor(R.color.main_color));
			receive_tv.setTextColor(getResources().getColor(R.color.black));
			finish_tv.setTextColor(getResources().getColor(R.color.black));

			break;
		case 3:
			all_view.setVisibility(View.GONE);
			pay_view.setVisibility(View.GONE);
			Ship_view.setVisibility(View.GONE);
			receive_view.setVisibility(View.VISIBLE);
			finish_view.setVisibility(View.GONE);
			all_tv.setTextColor(getResources().getColor(R.color.black));
			pay_tv.setTextColor(getResources().getColor(R.color.black));
			Ship_tv.setTextColor(getResources().getColor(R.color.black));
			receive_tv.setTextColor(getResources().getColor(R.color.main_color));
			finish_tv.setTextColor(getResources().getColor(R.color.black));

			break;
		case 4:
			all_view.setVisibility(View.GONE);
			pay_view.setVisibility(View.GONE);
			Ship_view.setVisibility(View.GONE);
			receive_view.setVisibility(View.GONE);
			finish_view.setVisibility(View.VISIBLE);
			all_tv.setTextColor(getResources().getColor(R.color.black));
			pay_tv.setTextColor(getResources().getColor(R.color.black));
			Ship_tv.setTextColor(getResources().getColor(R.color.black));
			receive_tv.setTextColor(getResources().getColor(R.color.black));
			finish_tv.setTextColor(getResources().getColor(R.color.main_color));

			break;
		default:
			break;
		}
	}

	public class MyOnPageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int arg0) {
			// TODO Auto-generated method stub

			tranSlate(arg0, 200);
			currIndex = arg0;
			setTextColorTitle(currIndex);
		}
	}

	// 平移动画
	public void tranSlate(int currentItem, long durationMillis) {
		float one = f;
		Animation animation = new TranslateAnimation(currIndex * one,
				currentItem * one, 0, 0);// 平移动画
		Log.v("currentoffest", "" + currIndex * one);
		Log.v("currentItem", "" + currentItem * one);
		animation.setFillAfter(true);// 动画终止时停留在最后一帧，不然会回到没有执行前的状态
		animation.setDuration(durationMillis);// 动画持续时间0.2秒
	}

	private void initRelative() {
		// TODO Auto-generated method stub
		all_tv.setOnClickListener(new relListener(0));
		pay_tv.setOnClickListener(new relListener(1));
		Ship_tv.setOnClickListener(new relListener(2));
		receive_tv.setOnClickListener(new relListener(3));
		finish_tv.setOnClickListener(new relListener(4));
	}

	public class relListener implements OnClickListener {
		private int index = 0;

		public relListener(int i) {
			this.index = i;
		}

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			viewpager.setCurrentItem(index);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = null;
		switch (v.getId()) {
//		case R.id.img_other:
//			intent = new Intent(getActivity(), ActivityCustomerService.class);
//			startActivity(intent);
//			break;
		case R.id.img_search:
			intent = new Intent(getActivity(), ActivityOrderSearch.class);
			startActivity(intent);
			// case R.id.title:
			// Toast.makeText(getActivity(), "123", 1).show();
		default:
			break;

		}

	}
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		IntentFilter intentFilterAvart4 = new IntentFilter("updateorder");
		getActivity().registerReceiver(updateorder, intentFilterAvart4);
		
	}
}
